//
//  TF_Modulation.swift
//  TalkfunSDKDemo
//
//  Created by 邓盛明 on 2022/1/11.
//  Copyright © 2022 Talkfun. All rights reserved.
//

import UIKit
import MediaPlayer
class TF_Modulation: NSObject {
    static let instance: TF_Modulation = TF_Modulation()
        
    class func shareInstance() -> TF_Modulation {
        return instance
    }

    var volumeViewSlider:UISlider? //系统音量调节


    //当前音量
    lazy var getCurrentVolume:CGFloat = {
        
         
        if (self.volumeViewSlider != nil ) {
            return CGFloat(self.volumeViewSlider!.value);
        }
        let volumeView = MPVolumeView()
        for i in 0..<volumeView.subviews.count {
            let view = volumeView.subviews[i]
            if(view.TF_className == "MPVolumeSlider")
            {
     
                self.volumeViewSlider = (view as! UISlider)
                break;
            }
        }
        
        // 解决初始状态下获取不到系统音量
        
        let audioSession = AVAudioSession.sharedInstance()
       
        
        return CGFloat(audioSession.outputVolume);
     }()
    
    
 //设置音量
    func setVolume(_ volume:CGFloat)  {
        
        var newVolume:CGFloat = volume
        newVolume = newVolume > 1 ? 1 : newVolume
        newVolume = newVolume < 0 ? 0 : newVolume
        if self.volumeViewSlider != nil {
            self.volumeViewSlider?.setValue(Float(newVolume), animated: false)
        }
       
    }
}
